# producer.py
from kafka import KafkaProducer
import os
import json

# 配置 Kafka SASL/PLAIN 认证
# os.environ['KAFKA_OPTS'] = '-Djava.security.auth.login.config=kafka_client_jaas.conf'

# Kafka 生产者配置
producer = KafkaProducer(
    bootstrap_servers=['localhost:9093'],  # Kafka 服务器地址和端口
    security_protocol='SASL_PLAINTEXT',    # 安全协议
    sasl_mechanism='PLAIN',                # SASL 机制
    sasl_plain_username='user',            # Kafka 用户名
    sasl_plain_password='user-secret',     # Kafka 密码
)

offenceData = {"resId":"65635353","queueId":"12342342","deviceId":"d232342342","isEnd":1,"offenceList":[{"offenceType":"1","offenceDesc":"逆行闯红灯","offenceTime":"2024-08-27 12:33:33","offenceVideo":"/offenceVideo.mp4","offencePics":["/offence1.png","/offence2.png"],"vehiclePlate":"渝A232J3","carInfo":{"vehicleType":"二轮摩托车，自行车，三轮车","color":"红色","brand":"兄弟牌"}}]}
# 序列化 JSON 数据
json_data = json.dumps(offenceData).encode('utf-8')
# str11 = '你好呀'
# 发送消息到 Kafka
# producer.send('test', b'Hello, Kafka with1134324 Python!')
producer.send('offence', json_data)
# producer.send('offence', b"dadsadads!")
producer.flush()

print("消息已发送到 Kafka 主题 'test'")
producer.close()
